export { };
const urls = require("./urls")
const navigate = require("./navigate")

exports.user_log_in = function user_log_in(that: any) {
    wx.login({
        success(res) {
            if (res.code) {
                //发起网络请求
                wx.request({
                    "url": urls.log_in_url,
                    "data": { "js_code": res.code },
                    success(res: any) {
                        console.log('login response :');
                        console.log(res.data);
                        wx.setStorageSync('app_user_info', res.data.data);
                        that.setData({
                            "app_user_info": res.data.data
                        })
                        if (that.data.app_user_info.isValidate == 0) {
                            wx.showToast({
                                title: '请尽快实名认证',
                                icon: 'error',
                                duration: 3000,
                                mask: true,
                            });
                            setTimeout(() => {
                                navigate.to_real_name_auth();
                            }, 3000);
                        }
                    }
                });
            } else {
                console.log('user_log_in fail ');
                console.log(res)
            }
        }
    })
};

exports.flush_user_info = function flush_user_info(that: any, call_back:Function) {
    wx.login({
        success(res) {
            if (res.code) {
                //发起网络请求
                wx.request({
                    "url": urls.log_in_url,
                    "data": { "js_code": res.code },
                    success(res: any) {
                        console.log('login response :');
                        console.log(res.data);
                        wx.setStorageSync('app_user_info', res.data.data);
                        that.setData({
                            "app_user_info": res.data.data
                        })
                        call_back(res);
                    }
                });
            } else {
                console.log('flush_user_info fail');
                console.log(res);
            }
        }
    })
};

interface real_name_auth_call_back {
    (res: any): void;
};

exports.real_name_auth = function real_name_auth(that: any, call_back: real_name_auth_call_back) {
    const app_user_info = wx.getStorageSync("app_user_info");

    wx.request({
        "url": urls.real_name_auth_url,
        "data": { "uuid": app_user_info.uuid },
        success(res: any) {
            call_back(res);
        },
        fail(res: any) {
            wx.showToast({
                title: '实名认证成失败',
                icon: 'error',
                duration: 3000,
                mask: true,
            });
            console.log('real_name_auth fail ');
            console.log(res);
        }
    });
};